package com.emuporium.gymrat.server.log;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emuporium.gymrat.server.data.Entry;
import com.emuporium.gymrat.server.data.LogDB;
import com.emuporium.gymrat.server.data.Routine;
import com.emuporium.gymrat.server.data.RoutineDB;
import com.emuporium.gymrat.server.data.User;
import com.emuporium.gymrat.server.data.UserDB;

@SuppressWarnings("serial")
public class LogAddEntrySubmitServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int routineId = new Integer(request.getParameter("routineid"));
		String dateParam = request.getParameter("date");
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		
		Date date;
		try {
			date = new Date(dateFormat.parse(dateParam).getTime());
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			date = null;
		}
		
		Routine routine = RoutineDB.selectRoutine(routineId);

		User user = UserDB.selectUser(request.getRemoteUser());

		for (Entry entry : routine.getEntries()) {
			ArrayList<Integer> reps = new ArrayList<Integer>();
			ArrayList<Integer> weight = new ArrayList<Integer>();
			String[] repsParam = request.getParameterValues("reps_"
					+ entry.getId());
			String[] weightParam = request.getParameterValues("weight_"
					+ entry.getId());

			int setSize;
			if (repsParam.length > weightParam.length) {
				setSize = repsParam.length;
			} else
				setSize = weightParam.length;

			for (int i = 0; i < setSize; i++) {
				if (!repsParam[i].equals("") && !weightParam[i].equals("")) {
					try {
						reps.add(new Integer(repsParam[i]));
						weight.add(new Integer(weightParam[i]));
					} catch (NumberFormatException e) {
						// one or the other contains non integer values, ignore
						// both
					}
				}

			}

			String variationString = request.getParameter("variation_"
					+ entry.getId());
			String equipmentString = request.getParameter("equipment_"
					+ entry.getId());

			int variation;
			int equipment;

			if (variationString == null)
				variation = 0;
			else
				variation = new Integer(variationString);

			if (equipmentString == null)
				equipment = 0;
			else
				equipment = new Integer(equipmentString);

			if (reps.size() != 0 && weight.size() != 0)
				LogDB.insertLog(routine.getId(), entry.getId(), entry
						.getExerciseId(), variation, equipment, date, reps,
						weight, user.getId());

		}
		request.setAttribute("redirecturl", "/log/view");
		String url = "/includes/redirect.jsp";
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
